package com.team7.service.impl;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.dao.CourseSubjectDao;
import com.team7.entity.Subject;
import com.team7.service.CourseManageService;
import com.team7.vo.CommonResult;
import com.team7.vo.CourseTypeVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: team7-object
 * @description:
 * @author: 田宏洋
 * @create: 2021-10-18 20:21
 **/
@Service
public class CourseManageServiceImpl extends ServiceImpl<CourseSubjectDao, Subject> implements CourseManageService {
    @Resource
    private CourseSubjectDao courseSubjectDao;
    @Override
    public CommonResult findCourseByPage(Integer page, Integer limit, CourseTypeVo courseTypeVo) {
        if(StringUtils.isNotEmpty(courseTypeVo.getTitle())||StringUtils.isNotEmpty(courseTypeVo.getStatusId())){
            Page<Subject> objectPage = new Page<>(page, limit);
            System.out.println(page+"yema");
            System.out.println(limit +"tiaoshu");
            QueryWrapper<Subject> objectQueryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotEmpty(courseTypeVo.getTitle())){
                objectQueryWrapper.like("title",courseTypeVo.getTitle());
            }
            if(StringUtils.isNotEmpty(courseTypeVo.getStatusId())){
                objectQueryWrapper.like("status_id",courseTypeVo.getStatusId());
            }
            objectQueryWrapper.eq("parent_id","0");
            Page<Subject> subjectPage = courseSubjectDao.selectPage(objectPage, objectQueryWrapper);
            List<Subject> records = subjectPage.getRecords();//shuju


            if(subjectPage.getTotal()==0){
                QueryWrapper<Subject> objectQueryWrapper1 = new QueryWrapper<>();
                if(StringUtils.isNotEmpty(courseTypeVo.getTitle())){
                    objectQueryWrapper1.like("title",courseTypeVo.getTitle());
                }
                if(StringUtils.isNotEmpty(courseTypeVo.getStatusId())){
                    objectQueryWrapper1.like("status_id",courseTypeVo.getStatusId());
                }
                Page<Subject> subjectPage1 = courseSubjectDao.selectPage(objectPage, objectQueryWrapper1);
                return new CommonResult(200,"查询成功",subjectPage1);

            }else {
                List<Subject> subjects = courseSubjectDao.selectList(null);
/*                System.out.println("-----------------");
                System.out.println(subjects);
                System.out.println("-------------------");*/
                for (Subject subject:records) {
                    subject.setChildren(findChildren(subjects,subject.getId()));
                }
                return new CommonResult(200,"查询成功",subjectPage);
            }
        }else {
            Page<Subject> objectPage = new Page<>(page, limit);
/*            System.out.println(page+"yema");
            System.out.println(limit +"tiaoshu");
            System.out.println(789);*/
            QueryWrapper<Subject> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("parent_id","0");
            Page<Subject> subjectPage = courseSubjectDao.selectPage(objectPage, objectQueryWrapper);
            List<Subject> records = subjectPage.getRecords();
            List<Subject> subjects = courseSubjectDao.selectList(null);
            System.out.println(records);
            for (Subject s:records) {
                s.setChildren(findChildren(subjects,s.getId()));
            }
            return new CommonResult(200,"查询成功",subjectPage);
        }

    }

    @Override
    public CommonResult insertCourseType(Subject subject) {
        int insertsubject = courseSubjectDao.insertCourseType(subject);
        if(insertsubject>0){
            return new CommonResult(200,"添加成功","OK");
        }else{
            return new CommonResult(500,"添加失败","NO");
        }
    }

    @Override
    public CommonResult updateCourseType(Subject subject) {
        int i = courseSubjectDao.updateById(subject);
        System.out.println("------------");
        System.out.println(subject);
        System.out.println(i);
        System.out.println("------------");
        if(i>0){
            return new CommonResult(200,"修改成功","OK");
        }else{
            return new CommonResult(500,"修改失败","NO");
        }

    }

    @Override
    public CommonResult deleteCourseType(String id) {
        List<Subject> subjects = courseSubjectDao.selectList(null);
        int i = deleteCourseType1(id, subjects);
        if (i>0){
            return new CommonResult(200,"删除成功","OK");
        }else {
            return new CommonResult(500,"删除失败","NO");
        }
    }

    @Override
    public CommonResult changeStatusId(CourseTypeVo courseTypeVo) {
        int i=0;
        if(courseTypeVo.getParentId().equals("0")&&courseTypeVo.getStatusId().equals("0")){
            courseSubjectDao.changeStatusId(courseTypeVo.getId(), courseTypeVo.getStatusId());
            QueryWrapper<Subject> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("parent_Id",courseTypeVo.getId());
            List<Subject> subjects = courseSubjectDao.selectList(objectQueryWrapper);
            for (Subject s:subjects) {
                s.setStatusId("0");
                i = courseSubjectDao.changeStatusId(s.getId(), s.getStatusId());

            }
            if(i>0){
                return new CommonResult(200,"修改状态成功","OK");
            }else {
                return new CommonResult(500,"修改状态失败","NO");
            }

        }else {
            i = courseSubjectDao.changeStatusId(courseTypeVo.getId(), courseTypeVo.getStatusId());
            if(i>0){
                return new CommonResult(200,"修改状态成功","OK");
            }else {
                return new CommonResult(500,"修改状态失败","NO");
            }
        }
    }

    @Override
    public CommonResult findsubjectByStatus(Integer page, Integer limit) {
        Page<Subject> objectPage = new Page<>();
        QueryWrapper<Subject> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("parent_id","0");
        objectQueryWrapper.eq("status_id","1");
        Page<Subject> subjectPage = courseSubjectDao.selectPage(objectPage, objectQueryWrapper);
        List<Subject> records = subjectPage.getRecords();
        List<Subject> subjects = courseSubjectDao.selectList(null);
        for (Subject s:records) {
            s.setChildren(findChildren1(subjects,s.getId()));
        }
        return new CommonResult(2000,"查询成功",subjectPage);
    }

    @Override
    public String findPidByid(String pid) {
        Subject subject = courseSubjectDao.selectById(pid);
        return subject.getParentId();
    }

    @Override
    public CommonResult getAllMenu() {
        QueryWrapper<Subject> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("parent_id","0");
        List<Subject> subjects = courseSubjectDao.selectList(objectQueryWrapper);
        List<Subject> subjects1 = courseSubjectDao.selectList(null);
        for (Subject s:subjects) {
            s.setChildren(findChildren(subjects1,s.getId()));
        }
        return new CommonResult(2000,"查询成功",subjects);
    }

    private int deleteCourseType1(String id,List<Subject> subjects){
        int i = courseSubjectDao.deleteById(id);
        for (Subject s:subjects) {
            if(id.equals(s.getParentId())){
                deleteCourseType1(s.getId(),subjects);
            }
        }
        return i;
    }

    //查询所有的不同级别的子课程分类(子类为非禁用状态的子类)
    private List<Subject> findChildren1(List<Subject> subjects,String pid){
        ArrayList<Subject> children = new ArrayList<>();
        for (Subject subject : subjects) {
            if (subject.getParentId().equals(pid)&&subject.getStatusId().equals("1")){
                children.add(subject);
            }
        }

        for (Subject child : children) {
            child.setChildren(findChildren(subjects,child.getId()));
        }

        return children;
    }

    //查询所有的不同级别的子课程分类
    private List<Subject> findChildren(List<Subject> subjects, String pid){
        ArrayList<Subject> children = new ArrayList<>();
        for (Subject subject : subjects) {
            if (subject.getParentId().equals(pid)){
                children.add(subject);
            }
        }
        for (Subject child : children) {
            child.setChildren(findChildren(subjects,child.getId()));
        }
        return children;
    }
}
